package com.camera2.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.camera2.http.Register;
import com.camera2.local.SharedPrefer;


public class LoginRegister extends Activity {
    private EditText et_number;
    private EditText et_password;
    private EditText et_password1;
    private EditText et_name;
    private String id, name, password, password1;


    private Handler handler1 = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    String info = (String) msg.obj;
                    if (info.equals("success")) {
                        try {
                            SharedPrefer.Saveid(getApplicationContext(), id);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        Toast.makeText(getApplicationContext(), "注册成功，请登录",  Toast.LENGTH_SHORT)
                                .show();
                        Intent intent = new Intent(getApplicationContext(),
                                ActivityLogin.class);
                        startActivity(intent);
                        finish();
                    }else Toast.makeText(getApplicationContext(), info, Toast.LENGTH_SHORT).show();
                    break;
                case 0:
                    Toast.makeText(getApplicationContext(), "注册失败，请稍后再试",  Toast.LENGTH_SHORT)
                            .show();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_register);

        et_number = findViewById(R.id.et_number);
        et_password = findViewById(R.id.et_password);
        et_password1 = findViewById(R.id.et_password1);
        et_name = findViewById(R.id.et_name);
    }


    public void register(View view) {

        id = et_number.getText().toString();
        password = et_password.getText().toString();
        password1 = et_password1.getText().toString();
        name = et_name.getText().toString();
        try {
            SharedPrefer.SaveUsername(getApplicationContext(), name);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        if (id.equals("")) {
            Toast.makeText(getApplicationContext(), "请输入手机号", Toast.LENGTH_SHORT).show();
        } else if (id.length() != 11) {
            Toast.makeText(getApplicationContext(), "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
        } else if (password.equals("")) {
            Toast.makeText(getApplicationContext(), "请输入密码", Toast.LENGTH_SHORT).show();
        } else if (name.equals("")) {
            Toast.makeText(getApplicationContext(), "请输入姓名", Toast.LENGTH_SHORT).show();
        } else if (!password.equals(password1)) {
            Toast.makeText(getApplicationContext(), "两次输入的密码不正确，请确认", Toast.LENGTH_SHORT).show();
        } else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Message msg = new Message();
                    try {
                        String info = Register.register(id, password, name);
                        msg.what = 1;
                        msg.obj = info;
                        handler1.sendMessage(msg);
                    } catch (Exception e) {
                        msg.what = 0;
                        handler1.sendMessage(msg);
                        e.printStackTrace();
                        return;
                    }
                }
            }).start();
        }

    }
}
